package org.swidgets.base.spanels;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseListenerCollection;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SourcesMouseEvents;


public class SSensibleSimplePanel extends SimplePanel implements SourcesMouseEvents{
	private MouseListenerCollection mouseListeners;
	private ClickListenerCollection clickListeners;
	public void initMouseEvent(){
		sinkEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP | Event.ONMOUSEOVER | Event.ONMOUSEOUT | Event.ONCLICK);		
	}
	public SSensibleSimplePanel(){
		super();
		initMouseEvent();
	}
	
	public void addMouseListener(MouseListener listener) {
		if (mouseListeners == null) {
		      mouseListeners = new MouseListenerCollection();
		    }
		    mouseListeners.add(listener);
	}

	public void removeMouseListener(MouseListener listener) {
		if (mouseListeners != null) {
		      mouseListeners.remove(listener);
		    }
	}
	
	public void addClickListener(ClickListener listener) {
	    if (clickListeners == null) {
	      clickListeners = new ClickListenerCollection();
	    }
	    clickListeners.add(listener);
	  }
	
	public void removeClickListener(ClickListener listener) {
	    if (clickListeners != null) {
	      clickListeners.remove(listener);
	    }
	  }
	
	public void onBrowserEvent(Event event) {
	    switch (DOM.eventGetType(event)) {
	    case Event.ONCLICK:
	    	if (clickListeners != null) {
	    		clickListeners.fireClick(this);
		        }
	    	
	      case Event.ONMOUSEDOWN:
	      case Event.ONMOUSEUP:
	      case Event.ONMOUSEOUT:
	      case Event.ONMOUSEOVER:
	        if (mouseListeners != null) {
	          mouseListeners.fireMouseEvent(this, event);
	        }
	        break;
	    }
	}

}
